package net.tribal.comic.services

import net.tribal.comic.entities.User


class AutenticateService {

    boolean transactional = true
	
	/**
	 * Validem el password donat el alias d'un usuari
	 * 
	 * @param alias
	 * @param password
	 * @return userInstance
	 */
    def validate ( alias, password ) {
    	def userInstance = new User()
    	
    	// Validem que existeixin l'alias i el password
		if ( !alias || !password ) {
			userInstance = new User()
			userInstance.errors.rejectValue("alias", "error.login.fault")
			userInstance.errors.rejectValue("password","")
			return userInstance
		}
		
    	// Busquem l'usuari
		userInstance = User.findWhere(alias:alias, password:password.encodeAsMD5())
		
		
		//	Si no existeix, afegim el motiu
		if ( !userInstance ) {
			userInstance = new User()
			userInstance.errors.rejectValue("alias", "error.login.fault")
			userInstance.errors.rejectValue("password","")
			return userInstance
		}
		
		return userInstance
    }
}